﻿#pragma once
#include"curl/curl.h"

#define HTTPREQUESTSUCCESSCALLBACK(name) void name(HttpRequestData *requestData, long httpCode,Client *client, void *data)
#define HTTPREQUESTFAILCALLBACK(name) void name(HttpRequestData *requestData,Client *client, void *data)

typedef struct HttpRequestData HttpRequestData;
typedef struct HttpRequestNode HttpRequestNode;
typedef struct HttpRequestList HttpRequestList;

void init_HttpRequest();
void init_HttpRequestList(HttpRequestList *list);
void add_httpRequestList(HttpRequestData *requestData, HttpRequestList *list);
void delet_httpRequestList(HttpRequestNode *requestNode);
void remove_httpRequestList(HttpRequestNode *requestNode, HttpRequestList *destination);
void httprequest_process();
void do_http(HttpRequestData *requestData);
void prepare_http_head(CURL *eh, HttpRequestData *requestData);
void init_response(HttpRequestData *requestData);
size_t httpWriteFunc(void *ptr, size_t size, size_t nmemb, HttpRequestData *requestData);
HttpRequestNode *get_dataFromRequestList(CURL *eh);
void httpRequest_solve();
void add_httpRequestReady(HttpRequestData *requestData);
void requestNode_cleanup(HttpRequestNode* node);
void requestData_cleanup(HttpRequestData *data);
int get_requestListLength(HttpRequestList *list);



struct HttpRequestData
{
	int requestType;//0是GET，1是POST，2是PUT，3是DELETE
	char *url;//访问地址
	char *data;//传输的数据，如json
	char **head;//http头部信息
	int head_num;//头部信息的数量
	char *response;//返回数据
	int response_size;//返回数据大小
	void(*successfinishCallFun)(HttpRequestData *requestData, long httpCode, Client *client,void *data);//成功请求结束的回调接口
	void(*failefinishCallFun)(HttpRequestData *requestData, Client *client, void *data);//失败请求结束的回调接口
	struct curl_slist *chunk;//保留为后续好销毁
	int finish;//此次查询是否完成，-1代表失败，0代表未完成，1代表成功
	char *error;//如果finish为-1时，此变量有效，存储错误信息
	CURL *eh;//保留句柄，为后续判断做判断
	void *userPoint;//回调函数需要传入的参数
	Client *client;
};

struct HttpRequestNode
{
	HttpRequestNode *next;
	HttpRequestNode *pre;
	HttpRequestData *data;
};
struct HttpRequestList
{
	HttpRequestNode head;
	HttpRequestNode end;
};